package problem20;
//20.有效括号
//https://leetcode.cn/problems/valid-parentheses/description/
import java.util.Stack;

class Solution {
    public boolean isValid(String s) {
        Stack<Character> stack = new Stack();
        for(char ch : s.toCharArray()) {
            if(ch == '(' || ch == '{' || ch == '[') {
                stack.push(ch);
            }else {
                if(stack.isEmpty()) return false;
                char t = stack.pop();
                System.out.println(t + " " + ch);
                if((t == '(' && ch != ')') || (t == '{' && ch != '}') || (t == '[' && ch != ']')) {
                    return false;
                }
            }
        }
        return stack.isEmpty();
    }
}